package api

import (
	"github.com/EDDYCJY/go-gin-example/models"
	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/EDDYCJY/go-gin-example/routers/internal"

	"fmt"

	"github.com/gin-gonic/gin"
)

type CalculatePriceRequest struct {
	InstanceName    string
	LeaseTimeSecond int //unit second
}

type CalculatePrice struct {
	Amount float64 `json:"amount"`
}

// 计算价格
func Handle_Calculate_Price(c *gin.Context) {
	appG := app.Gin{C: c}
	calculatePriceRequest := &CalculatePriceRequest{}
	c.BindQuery(&calculatePriceRequest)
	// Check if required query parameters are present
	if calculatePriceRequest.InstanceName == "" || calculatePriceRequest.LeaseTimeSecond <= 0 {
		appG.ResponseError(-1, fmt.Errorf("请填写InstanceName和LeaseTimeSecond参数,LeaseTimeSecond需要大于0 ").Error())
		return
	}

	//calculate the price
	db := models.GetDB()
	amount, err := internal.CalculatePrice(db, calculatePriceRequest.InstanceName, calculatePriceRequest.LeaseTimeSecond)
	if err != nil {
		appG.ResponseError(-1, err.Error())
		return
	}
	appG.ResponseSuccess(&CalculatePrice{Amount: amount})
}
